package com.myebills.ebpsfrontend.cust.maxis;

import java.sql.*;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.myebills.ebpsfrontend.cust.controller.*;
import javax.servlet.http.HttpSession;
import com.ibm.logging.ILogger;
import com.ibm.logging.IRecordType;

public class CusAddPhoneBookPopUpServlet_v1r00 extends EbppxTransactionServlet {

    public void doTransaction(HttpServletRequest req, HttpServletResponse res) throws ServletException {
        ILogger trcLogger = null;
        trcLogger = getTraceLogger(req);
        final String fName = "doTransaction";
        if (trcLogger.isLogging())
            trcLogger.entry(IRecordType.TYPE_INFO, this, fName);

        final HttpSession session = req.getSession(false);
        if (null == session)
                throw new IllegalStateException("no session established");

        //Get attribute from session for the logon user
        final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);

        try {
                Collection resultList = null;
                String number = req.getParameter("number");
                String name = req.getParameter("name");
                String callType = req.getParameter("callType");

		        String updateMsgCss = req.getParameter("updateMsgCss");
		        System.out.println("updateMsgCss in servlet is "+updateMsgCss);

                boolean boolResult = false;

                resultList = PhoneBookDAO.getInstance().getContact(ecsUser.getUserId(), number);

                if(resultList.size()<1){
					// Add
					boolResult = PhoneBookDAO.getInstance().add(ecsUser.getUserId(), number, name, callType);

					if(boolResult){
						req.setAttribute("PHONEBOOK_ACTION", "ADD");
						resultList = PhoneBookDAO.getInstance().getContact(ecsUser.getUserId(), number);
					}
				}else{
					// Update
					boolResult = PhoneBookDAO.getInstance().update(ecsUser.getUserId(), number, name, callType);
					if(boolResult){
						req.setAttribute("PHONEBOOK_ACTION", "UPDATE");
						resultList = PhoneBookDAO.getInstance().getContact(ecsUser.getUserId(), number);
					}
				}
                req.setAttribute("PhoneBookList", resultList);
		        req.setAttribute(EbppxConstants.UPDATE_MSG_CSS, updateMsgCss);

                forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);

            }catch (Throwable t){
                throw new ServletException(t.toString(), t);
            }finally {

	    } //finally

    }//doTransaction



}
